
** South Africa
** Clean Wave 3

*************
*************

	** ROSTER
		use "$dir/rawdata/NIDS South Africa/wave3/HouseholdRoster_W3_Anon_V3.0.0.dta", clear

		gen w3_inhh=w3_r_pres==1 if !missing(w3_r_pres)
					
	** Household size
		gen temp=1 if w3_inhh==1
		bys w3_hhid: egen w3_hh_size=total(temp)
		drop temp
		
	** Marital status
		gen w3_married=w3_r_marstt==1 if !missing(w3_r_marstt)

	** Save
		keep w3_hhid pid w3_inhh w3_hh_size w3_married
		tempfile roster
		save `roster', replace
		
*************
*************

	** INDIVIDUAL FILES (DERIVED)
		use "$dir/rawdata/NIDS South Africa/wave3/indderived_W3_Anon_V3.0.0.dta", clear
	
	** Female
		gen w3_female=w3_best_gen==2 if !missing(w3_best_gen) & w3_best_gen>0
		
	** Age
		gen w3_age=w3_best_age_yrs
		
	** Works
		gen w3_work_any_c=w3_empl_stat==3 if !missing(w3_empl_stat)
		
		keep w3_hhid pid w3_age w3_work_any_c w3_female 
		tempfile indderiv
		save `indderiv', replace
			 
	** INDIVIDUAL FILES
		use "$dir/rawdata/NIDS South Africa/wave3/Adult_W3_Anon_V3.0.0.dta", clear
		
	** Employment
		gen w3_work_primaryocc=w3_a_em1hrs>0 if !missing(w3_a_em1hrs)
		gen w3_work_secondaryocc=w3_a_em2==1 if !missing(w3_a_em2) & w3_a_em2>0
		gen w3_work_selfemp=w3_a_ems==1 if !missing(w3_a_ems) & w3_a_ems>0
		gen w3_work_casual=w3_a_emc==1 if !missing(w3_a_emc) & w3_a_emc>0
		gen w3_work_agri=w3_a_emp==1 if !missing(w3_a_emp) & w3_a_emp>0
		gen w3_work_bizhelp=w3_a_emh==1 if !missing(w3_a_emh) & w3_a_emh>0
		
		egen w3_work_any=rowmax(w3_work_primaryocc w3_work_secondaryocc w3_work_selfemp w3_work_casual w3_work_agri w3_work_bizhelp)

	** Work hours
		foreach var in w3_a_em1hrs w3_a_em2hrs w3_a_emshrs w3_a_emchrs w3_a_emphrs w3_a_emhhrs {
			replace `var'=. if `var'<0
		}
		egen w3_work_hrs=rowtotal(w3_a_em1hrs w3_a_em2hrs w3_a_emshrs w3_a_emchrs w3_a_emphrs w3_a_emhhrs)
	
	** CESD
		foreach var in w3_a_emobth w3_a_emomnd w3_a_emodep w3_a_emoeff w3_a_emohope w3_a_emofear w3_a_emoslp w3_a_emohap w3_a_emolone w3_a_emogo {
			recode `var' -8=. -3=. -9=.
			recode `var' 1=0 2=1 3=2 4=3
		}
		replace w3_a_emohope=3-w3_a_emohope
		replace w3_a_emohap=3-w3_a_emohap
		egen w3_cesd_total=rowtotal(w3_a_emobth w3_a_emomnd w3_a_emodep w3_a_emoeff w3_a_emohope w3_a_emofear w3_a_emoslp w3_a_emohap w3_a_emolone w3_a_emogo)
		egen w3_cesd_count=rownonmiss(w3_a_emobth w3_a_emomnd w3_a_emodep w3_a_emoeff w3_a_emohope w3_a_emofear w3_a_emoslp w3_a_emohap w3_a_emolone w3_a_emogo)
		replace w3_cesd_total=. if w3_cesd_count<8 // don't score if more than 2 are missing
		
		gen w3_depressed=w3_cesd_total>=10
		
		
	** DISABILITIES
		tab1 w3_a_hlvisaid w3_a_hlhraid 
		tab1 w3_a_hlvisaid w3_a_hlhraid, nol

	** Difficulty seeing
		gen w3_diff_seei = w3_a_hlvisaid
		replace w3_diff_seei = 0 if w3_a_hlvisaid==2
		replace w3_diff_seei = . if w3_a_hlvisaid < 0
		tab1 w3_diff_seei

	** Difficulty hearing
		gen w3_diff_hear = w3_a_hlhraid
		replace w3_diff_hear = 0 if w3_a_hlhraid==2
		replace w3_diff_hear = . if w3_a_hlhraid < 0
		tab1 w3_diff_hear

	** Difficulty walking or climbing stairs

		*na


	** Difficulty remembering or concentrating
		*na

	** Difficulty with self-care

		*na

	** Difficulty communicating

		*na

		keep w3_hhid pid w3_work_any w3_work_hrs w3_work_primaryocc w3_work_secondaryocc w3_work_selfemp w3_work_casual w3_work_agri w3_work_bizhelp w3_cesd_count w3_depressed w3_diff_seei w3_diff_hear
		tempfile indiv
		save `indiv', replace		
*************
*************

	** HOUSEHOLD DATA 

		use "$dir/rawdata/NIDS South Africa/wave3/hhderived_W3_Anon_V3.0.0.dta", clear
		
		gen w3_urban=w3_geo2011==2 if !missing(w3_geo2011)
		
		gen w3_hh_size_check=w3_hhsizer
		keep w3_hhid w3_hh_size_check w3_urban
			
		tempfile hh
		save `hh', replace
		
*************
*************

	** APPEND
		
		use `hh', clear
		merge 1:m w3_hhid using `roster', assert(matched) nogen
		merge 1:1 w3_hhid pid using `indderiv', assert(master matched) gen(merge_indderiv)
		merge 1:1 w3_hhid pid using `indiv', assert(master matched) gen(merge_indiv)
		
		order w3_hhid pid
		drop merge*
		
		compare w3_work_any_c w3_work_any
		drop w3_work_any_c
		compare w3_hh_size_check w3_hh_size
		drop w3_hh_size_check
		
		save "$dir/data/saf_w3_individual.dta", replace
		
	** Missing variables: 
	** Urban status
	
	** Illness and injury

	** Impact of illness and injury
		